package com.graduation.springbootinit.controller;

import com.graduation.springbootinit.common.BaseResponse;
import com.graduation.springbootinit.model.entity.ShopPart.Order;
import com.graduation.springbootinit.model.vo.OrderDetailVO;
import com.graduation.springbootinit.service.OrderService;
import com.graduation.springbootinit.service.UserService;
import com.graduation.springbootinit.model.dto.order.OrderCreateRequest;
import com.graduation.springbootinit.model.dto.order.CartOrderCreateRequest;
import com.graduation.springbootinit.model.dto.order.StoreOrderQueryRequest;
import com.graduation.springbootinit.model.dto.product.ParamIdRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/shopPart/order")
public class OrderController {

    @Resource
    private OrderService orderService;

    @Resource
    private UserService userService;

    @PostMapping("/create")
    public BaseResponse<Order> createOrder(@RequestBody OrderCreateRequest orderCreateRequest, HttpServletRequest request) {
        Long userId = userService.getLoginUser(request).getId();
        return orderService.createOrder(userId, 
            orderCreateRequest.getProductId(), 
            orderCreateRequest.getQuantity(), 
            orderCreateRequest.getAddressId(),
            orderCreateRequest.getSize());
    }

    @PostMapping("/createFromCart")
    public BaseResponse<Order> createOrderFromCart(@RequestBody CartOrderCreateRequest cartOrderCreateRequest, HttpServletRequest request) {
        Long userId = userService.getLoginUser(request).getId();
        return orderService.createOrderFromCart(userId, 
            cartOrderCreateRequest.getCartId(), 
            cartOrderCreateRequest.getAddressId());
    }

    @PostMapping("/get")
    public BaseResponse<Order> getOrder(@RequestBody ParamIdRequest idRequest) {
        return orderService.getOrderById(idRequest.getId());
    }

    @GetMapping("/list")
    public BaseResponse<List<Order>> getUserOrders(
            @RequestParam(required = false) Integer status,
            HttpServletRequest request) {
        Long userId = userService.getLoginUser(request).getId();
        return orderService.getUserOrders(userId, status);
    }

    @PostMapping("/cancel")
    public BaseResponse<Boolean> cancelOrder(@RequestBody ParamIdRequest idRequest) {
        return orderService.cancelOrder(idRequest.getId());
    }

    @PostMapping("/pay")
    public BaseResponse<Boolean> payOrder(@RequestBody Order order) {
        return orderService.payOrder(order.getId());
    }

    @PostMapping("/confirm")
    public BaseResponse<Boolean> confirmReceive(@RequestBody Order order) {
        return orderService.confirmReceive(order.getId());
    }

    @PostMapping("/detail")
    public BaseResponse<OrderDetailVO> getOrderDetail(@RequestBody Order order, HttpServletRequest request) {
        userService.getLoginUser(request); // 验证用户是否登录
        return orderService.getOrderDetail(order.getId());
    }

    @PostMapping("/ship")
    public BaseResponse<Boolean> shipOrder(@RequestBody Order order) {
        return orderService.shipOrder(order.getId());
    }

    /**
     * 获取商户未发货订单列表
     */
    @PostMapping("/store/unshipped")
    public BaseResponse<List<Order>> getUnshippedOrdersByStore(@RequestParam Long storeId) {
        return orderService.getUnshippedOrdersByStore(storeId);
    }

    @ApiOperation("获取商户订单列表")
    @PostMapping("/store/listByStatus")
    public BaseResponse<List<Order>> getStoreOrdersByStatus(@RequestBody StoreOrderQueryRequest request) {
        return orderService.getOrdersByStore(request.getStoreId(), request.getStatus());
    }
} 